package com.vvlive.caibao.smartcity.fragment;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.ListView;

import com.vvlive.caibao.smartcity.adapter.MeetingsCtrlAdapter;
import com.vvlive.caibao.smartcity.base.BaseFragment;
import com.vvlive.caibao.smartcity.base.ListHttpCallBack;
import com.vvlive.caibao.smartcity.bean.MeetingByMonth;
import com.vvlive.caibao.smartcity.bean.PersonalMeetingBean;
import com.vvlive.caibao.smartcity.ui.EditMeetingActivity;
import com.vvlive.caibao.smartcity.ui.OrderOneMeetingActivity;
import com.vvlive.caibao.smartcity.utils.MeetingsController;
import com.vvlive.caibao.smartcity.utils.ViewStorage;
import com.vvlive.caibao.smartcity.view.SlideCtrlView;
import com.vvlive.caibao.smartcity.view.SlideCtrlView.OnCtrlClickListener;

import java.util.ArrayList;
import java.util.List;

import smartcity.vvlive.caibao.com.smartc.R;

/**
 * Created by zhj on 2015/9/21.
 */
public class PersonalMeetingsFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {

    private SwipeRefreshLayout mMeetingsRefresh;
    private ListView mList;
    private MeetingsCallBack mCallBack;
    private Intent mIntent;
    private MeetingsController mController;

    @Override
    public int getContentRes() {
        return R.layout.ui_personal_meetings;
    }

    @Override
    public void initFragment() {
        mMeetingsRefresh = this.findView(R.id.meetings_refresh);
        mMeetingsRefresh.setOnRefreshListener(this);
        mList = this.findView(R.id.meetings_list);
        View emptyView = ViewStorage.setListEmptyView(getActivity(), mList, R.layout.view_meeting_empty);
        emptyView.findViewById(R.id.order_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(getIntentByClass(OrderOneMeetingActivity.class));
            }
        });
        onRefresh();
    }

    /**
     * 获得会议操作回调单例
     *
     * @return
     */
    private MeetingsCallBack getCallBack() {
        if (mCallBack == null) {
            mCallBack = new MeetingsCallBack();
        }
        return mCallBack;
    }

    @Override
    public void onRefresh() {
        getController().fetchMyMeetings(getCallBack());
    }

    /**
     * 会议的操作回调函数
     */
    final class MeetingsCallBack extends ListHttpCallBack<PersonalMeetingBean>
            implements OnCtrlClickListener, MeetingsCtrlAdapter.OnItemClickListener {

        public static final int DETAILS_REQUEST_CODE = 100;
        public static final int DETAILS_RESULT_DELETE_CODE = 102;

        private ArrayList<PersonalMeetingBean> mMeetingsList;
        private MeetingsCtrlAdapter mCtrlAdapter;

        private ProgressDialog mDialog;
        private int mPosition;
        private SlideCtrlView mCtrl;

        public MeetingsCallBack() {
            mMeetingsList = new ArrayList<>();
        }

        @Override
        public void onEdit(SlideCtrlView v) {
            mCtrl = v;
            mPosition = getPosition(v);
            v.smoothScrollTo(0, 0);

            MeetingByMonth meeting = toMeetingByMonth(mMeetingsList.get(mPosition));
            Intent intent = getIntentByClass(EditMeetingActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable("obj", meeting);
            intent.putExtras(bundle);
            startActivity(intent);
        }

        @Override
        public void onDelete(SlideCtrlView v) {
            mCtrl = v;
            v.smoothScrollTo(0, 0);
        }

        /**
         * 获得当前操作的是第几条
         *
         * @param view
         * @return
         */
        private int getPosition(SlideCtrlView view) {
            return view != null ? (int) view.getData() : -1;
        }

        @Override
        public void onSuccess(List<PersonalMeetingBean> list) {
            mMeetingsRefresh.setRefreshing(false);
            mMeetingsList = (ArrayList<PersonalMeetingBean>) list;
            mCtrlAdapter = new MeetingsCtrlAdapter(getActivity(), list);
            mCtrlAdapter.setOnItemClickListener(this);
            mList.setAdapter(mCtrlAdapter);
            mCtrlAdapter.setOnCtrlListener(this);
        }

        @Override
        public void onError(int errCode, String str) {
            mMeetingsRefresh.setRefreshing(false);
            toast("操作错误，请重试");
        }

        /**
         * PersonalMeetingBean转换成MeetingByMonth
         *
         * @param bean
         * @return
         */
        private MeetingByMonth toMeetingByMonth(PersonalMeetingBean bean) {
            MeetingByMonth meeting = new MeetingByMonth();
            meeting.setRoom(bean.getRoom());
            meeting.setRoomid(bean.getRoomid());
            meeting.setTopic(bean.getTopic());
            meeting.setTime(bean.getTime());
            meeting.setDeptname(bean.getDeptname());
            meeting.setMeetingid(bean.getMeetingid());
            meeting.setName(bean.getName());
            return meeting;
        }

        @Override
        public void onItemClick(SlideCtrlView view, int position) {

            if (!view.isCtrlShown()) {
                MeetingByMonth meeting = toMeetingByMonth(mMeetingsList.get(position));
                Intent intent = getIntentByClass(EditMeetingActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("obj", meeting);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        }
    }

    /**
     * 获得会议操作的单例
     *
     * @return
     */
    private MeetingsController getController() {
        if (mController == null) {
            mController = new MeetingsController();
        }
        return mController;
    }

    /**
     * 根据Class获得一个Intent
     *
     * @param cls 要跳转的Activity的Class
     * @return
     */
    private Intent getIntentByClass(Class cls) {
        if (mIntent == null) {
            mIntent = new Intent();
        }
        mIntent.setClass(getActivity(), cls);
        return mIntent;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == MeetingsCallBack.DETAILS_REQUEST_CODE) {
            if (resultCode == MeetingsCallBack.DETAILS_RESULT_DELETE_CODE) { // 删除后返回
                onRefresh();
            }
            if (resultCode == 300) {    // 编辑后返回
                onRefresh();
            }
            if (resultCode == 1000) {   // 添加后返回
                onRefresh();
            }
        }

    }

}
